/*
 * sparse.c
 *
 *  Created on: May 3, 2009
 *      Author: sears
 */

#include "sparse.h"
#include <string.h>

int cmp_128bit(const struct uint128_t arryA,
			   const struct uint128_t arryB) {
	return memcmp(arryA.buf, arryB.buf, sizeof(arryA.buf));
}

double jaccard_distance_sparse(const void * ap, const void * bp) {
	const struct uint128_vec * av = ap;
	const struct uint128_vec * bv = bp;
	const struct uint128_t * a = av->vals;
	intptr_t alen = av->len;
	const struct uint128_t * b = bv->vals;
	intptr_t blen = bv->len;
	intptr_t aoff = 0;
	intptr_t boff = 0;
	intptr_t intersect = 0;
	intptr_t theUnion = 0;
	while(aoff < alen && boff < blen) {
		int cmp = cmp_128bit(a[aoff], b[boff]);
		if(cmp < 0) { /*a < b*/
			theUnion++;
			aoff++;
		} else if(cmp > 0) { /*a > b*/
			theUnion++;
			boff++;
		} else {
			theUnion++;
			intersect++;
			aoff++;
			boff++;
		}
	}
	theUnion += (alen-aoff);
	theUnion += (blen-boff);
	return 1.0-((double)intersect)/((double)theUnion);
}
